package com.sakura.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sakura.common.BaseContext;
import com.sakura.common.R;
import com.sakura.pojo.ShoppingCart;
import com.sakura.serivce.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        // 查询购物车中是否有商品
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id", BaseContext.getCurrentId());
        shoppingCartQueryWrapper.eq(dishId != null, "dish_id", dishId);
        shoppingCartQueryWrapper.eq(setmealId != null, "setmeal_id", setmealId);

        ShoppingCart databaseShoppingCart = shoppingCartService.getOne(shoppingCartQueryWrapper);
        if (databaseShoppingCart == null) {
            // 如果没有购物车中没有商品中没有则添加
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        } else {
            //购物车中有则加1
            databaseShoppingCart.setNumber(databaseShoppingCart.getNumber() + 1);
            shoppingCartService.updateById(databaseShoppingCart);
        }
        return R.success("添加成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        // 查询购物车中是否有商品
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id", BaseContext.getCurrentId());
        shoppingCartQueryWrapper.eq(dishId != null, "dish_id", dishId);
        shoppingCartQueryWrapper.eq(setmealId != null, "setmeal_id", setmealId);

        ShoppingCart databaseShoppingCart = shoppingCartService.getOne(shoppingCartQueryWrapper);

        databaseShoppingCart.setNumber(databaseShoppingCart.getNumber() - 1);

        if (databaseShoppingCart.getNumber() == 0) {
            shoppingCartService.removeById(databaseShoppingCart.getId());
        } else {
            shoppingCartService.updateById(databaseShoppingCart);
        }
        return R.success("添加成功");
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id", userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list();
        return R.success(shoppingCartList);
    }

    @DeleteMapping("/clean")
    public R<String> clean() {
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id", BaseContext.getCurrentId());
        shoppingCartService.remove(shoppingCartQueryWrapper);
        return R.success("删除成功");
    }
}
